﻿Shader "Kun URP/ToonLit"
{
    Properties
    {
        [HDR][MainColor] _MainColor("Main Color", Color) = (1, 1, 1, 1)
        [MainTexture] _MainTex ("Main Tex", 2D) = "white" {}
        _ILMTex ("ILM Tex", 2D) = "white" {}

        [KunMain(Shadow, true, false)] _Shadow_Enable ("Shadow On or Off", float) = 1
        [KunSub(Shadow)][HDR] _ShadowColor ("Shadow Color", Color) = (0.7, 0.7, 0.7, 1)
        [KunSub(Shadow)] _ShadowRange ("Shadow Range", Range(0, 1)) = 0.6
        [KunSub(Shadow)] _ShadowSmooth ("Shadow Smooth", Range(0, 0.03)) = 0.002

        [KunMain(Specular, true, false)] _Specular_Enable ("Specular On or Off", float) = 1
        [KunSub(Specular)] _SpecularRange ("Specular Range", Range(0, 1)) = 0.998
		[KunSub(Specular)] _SpecularMulti ("Specular Multi", Range(0, 1)) = 0.4
        [KunSub(Specular)] _SpecularGloss ("Specular Gloss", Range(0.001, 8)) = 4

        [KunMain(Outline, true, true)] _Outline_Enable ("Outline On or Off", float) = 1
        [KunSub(Outline)] _OutlineColor ("Outline Color", Color) = (0.5, 0.5, 0.5, 1)
        [KunSub(Outline)] _OutlineWidth ("Outline Width", Range(0, 0.1)) = 0.0015

        [KunMain(Rim, true, true)] _Rim_Enable ("Rim On or Off", float) = 1
        [KunSub(Rim)][HDR] _RimColor ("Rim Color", Color) = (1, 1, 1, 1)
        [KunSub(Rim)] _RimMin ("Rim Min", Range(0, 1)) = 1
        [KunSub(Rim)] _RimMax ("Rim Max", Range(0, 1)) = 1
        [KunSub(Rim)] _RimSmooth ("Rim Smooth", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags 
        {
            "RenderPipeline" = "UniversalPipeline"
        }

        Pass
        {
            Name "SurfaceColor"
            Tags
            {
                // "Lightmode" tag must be "UniversalForward" in order to render lit objects in URP.
                "LightMode" = "UniversalForward"
            }

            HLSLPROGRAM
            
            // -------------------------------------
            // Universal Render Pipeline keywords
            // When doing custom shaders you most often want to copy and paste these #pragmas
            // These multi_compile variants are stripped from the build depending on:
            // 1) Settings in the URP Asset assigned in the GraphicsSettings at build time
            // e.g If you disabled AdditionalLights in the asset then all _ADDITIONA_LIGHTS variants
            // will be stripped from build
            // 2) Invalid combinations are stripped. e.g variants with _MAIN_LIGHT_SHADOWS_CASCADE
            // but not _MAIN_LIGHT_SHADOWS are invalid and therefore stripped.
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
            #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
            #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
            #pragma multi_compile _ _SHADOWS_SOFT
            // -------------------------------------
            // Unity defined keywords
            #pragma multi_compile_fog
            // -------------------------------------
            #pragma shader_feature_local _ _RIM_ENABLE_ON

            #pragma vertex ToonLitForwardPassVertex
            #pragma fragment ToonLitForwardPassFragment

            //all shader logic written inside this .hlsl
            #include "ToonLitForwardPass.hlsl"

            ENDHLSL
        }

        Pass
        {
            Name "Outline"
            Tags 
            {
                //"LightMode" = "UniversalForward" // IMPORTANT: don't write this line for any custom +pass! else this outline pass will not be rendered in URP!
            }
            Cull Front // Cull Front is a must for extra pass outline method

            HLSLPROGRAM

            // -------------------------------------
            // Universal Render Pipeline keywords
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
            #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
            #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
            #pragma multi_compile _ _SHADOWS_SOFT
            // -------------------------------------
            // Unity defined keywords
            #pragma multi_compile_fog
            // -------------------------------------

            #pragma shader_feature_local _ _OUTLINE_ENABLE_ON

            #pragma vertex ToonOutlinePassVertex
            #pragma fragment ToonOutlinePassFragment

            //all shader logic written inside this .hlsl
            #include "ToonOutlinePass.hlsl"

            ENDHLSL
        }

        // Used for rendering URP's shadowmaps
        Pass
        {
            Name "ShadowCaster"
            Tags{"LightMode" = "ShadowCaster"}

            ZWrite On
            ZTest LEqual
            Cull[_Cull]

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature _ALPHATEST_ON

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing
            #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment

            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
            ENDHLSL
        }

        // Used for depth prepass
        // If depth texture is needed, we need to perform a depth prepass for this shader. 
        Pass
        {
            Tags{"LightMode" = "DepthOnly"}

            ZWrite On
            ColorMask 0

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            #pragma vertex DepthOnlyVertex
            #pragma fragment DepthOnlyFragment

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature _ALPHATEST_ON

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing

            #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
            ENDHLSL
        }
    }
}
